package com.xmqian.app.project.ui.user.activity

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import com.coszero.common.base.mvp.BaseMvpActivity
import com.coszero.ui.dialog.ToastUtils
import com.coszero.ui.widget.CountDownTextView
import com.example.xmqian.R
import com.example.xmqian.databinding.ActivityLoginQuickBinding
import com.xmqian.app.project.event.LoginEvent
import com.xmqian.app.project.ui.main.MainActivity
import com.xmqian.app.project.ui.user.contract.LoginContract
import com.xmqian.app.project.ui.user.model.LoginModel
import com.xmqian.app.project.ui.user.presenter.LoginPresenter
import org.greenrobot.eventbus.EventBus

class QuickLoginActivity : BaseMvpActivity<LoginPresenter?>(), LoginContract.View {
    var mTvGetCode: CountDownTextView? = null
    var mBtnLogin: Button? = null
    var mEtUser: EditText? = null
    var mEtPwd: EditText? = null
    var textWatcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            setLoginBtnState()
        }

        override fun afterTextChanged(s: Editable) {
        }
    }
    private var binding: ActivityLoginQuickBinding? = null


    override fun getLayoutView(): LinearLayout {
        setShowBack(false)
        title = "快捷登录"
        binding = ActivityLoginQuickBinding.inflate(layoutInflater)
        return binding!!.root
    }

    override fun getPresenter(): LoginPresenter {
        return LoginPresenter(this)
    }

    override fun initView() {
        mEtPwd = binding!!.etSmsCode
        mEtUser = binding!!.etUser
        mEtPwd!!.addTextChangedListener(textWatcher)
        mEtUser!!.addTextChangedListener(textWatcher)
    }

    override fun initData() {
    }

    /**
     * 注册
     *
     * @param view
     */
    fun register(view: View?) {
        startActivity(Intent(this, RegisterActivity::class.java))
    }

    private val smsCode: Unit
        get() {
            if (mEtUser!!.length() == 11) {
                mPresenter!!.getSmsCode2(3, mEtUser!!.text.toString())
            } else {
                ToastUtils.showMsg("手机号长度不正确")
            }
        }

    fun onClick(v: View) {
        if (v.id == R.id.btn_login) { // 登录
            val userName = mEtUser!!.text.toString()
            val passWord = mEtPwd!!.text.toString()
            mPresenter!!.quickLogin(userName, passWord)
        } else if (v.id == R.id.tv_get_code) { // 获取验证码
            smsCode
        } else if (v.id == R.id.tv_pass_login) { // 跳转密码登录
            LoginActivity.Companion.startTask(this)
        }
    }

    override fun onNetworkError() {
        hideLoadingDialog()
    }

    fun setLoginBtnState() {
        if (checkUserInput()) {
            mBtnLogin!!.isEnabled = true
        } else {
            mBtnLogin!!.isEnabled = false
        }
    }

    fun checkUserInput(): Boolean {
        return mEtUser!!.length() == 11 && mEtPwd!!.length() >= 4
    }

    override fun onLoginSucceed(data: LoginModel) {
        ToastUtils.showMsg("登录成功")
        startActivity(Intent(this, MainActivity::class.java))
        EventBus.getDefault().post(LoginEvent(true))
    }

    override fun onRequestFailed(message: String?) {
    }

    override val codeSuccess: Unit
        get() {
            //验证码发送成功
        }
}
